package net.lab1024.sa.admin.module.business.exam.service;

import cn.idev.excel.FastExcel;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.exam.dao.QuestionsDao;
import net.lab1024.sa.admin.module.business.exam.dao.QuestionsaDao;
import net.lab1024.sa.admin.module.business.exam.domain.entity.QuestionsEntity;
import net.lab1024.sa.admin.module.business.exam.domain.entity.QuestionsaEntity;
import net.lab1024.sa.admin.module.business.exam.domain.form.add.QuestionsAddForm;
import net.lab1024.sa.admin.module.business.exam.domain.form.query.QuestionsQueryForm;
import net.lab1024.sa.admin.module.business.exam.domain.form.update.QuestionsUpdateForm;
import net.lab1024.sa.admin.module.business.exam.domain.vo.QuestionsVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 课程分类 Service
 *
 * @Author AKITA
 * @Date 2025-06-06 11:08:34
 * @Copyright AKITA
 */

@Service
public class QuestionsService {

    @Resource
    private QuestionsDao questionsDao;
    @Autowired
    private QuestionsaDao questionsaDao;
    @Autowired
    private QuestionsaService questionsaService;

    public PageResult<QuestionsVO> queryPage(QuestionsQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<QuestionsVO> list = questionsDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    @Transactional
    public ResponseDTO<Long> add(QuestionsAddForm addForm) {
        QuestionsEntity questionsEntity = SmartBeanUtil.copy(addForm, QuestionsEntity.class);
        questionsDao.insert(questionsEntity);
        for (QuestionsaEntity questionsaEntity : addForm.getLista()) {
            questionsaDao.insert(questionsaEntity);
        }
        return ResponseDTO.ok(questionsEntity.getDocEntry());
    }

    public ResponseDTO<String> update(QuestionsUpdateForm updateForm) {
        QuestionsEntity questionsEntity = SmartBeanUtil.copy(updateForm, QuestionsEntity.class);
        if (questionsDao.updateById(questionsEntity) == 0) {
            throw new RuntimeException("数据已被更新，刷新后重试！");
        }
        questionsaDao.deleteById(questionsEntity.getDocEntry());
        for (QuestionsaEntity questionsaEntity : updateForm.getLista()) {
            questionsaDao.insert(questionsaEntity);
        }
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        questionsDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    @Transactional
    public ResponseDTO<String> delete(Long docEntry) {
        questionsDao.deleteById(docEntry);
        questionsaDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }

    public ResponseDTO<QuestionsEntity> info(Long docEntry) {
        QuestionsEntity questionsEntity = questionsDao.selectById(docEntry);
        questionsEntity.setLista(questionsaDao.selectList(new LambdaQueryWrapper<QuestionsaEntity>().eq(true, QuestionsaEntity::getDocEntry, docEntry)));
        return ResponseDTO.ok(questionsEntity);
    }


    public ResponseDTO<String> imPort(MultipartFile file) {
        List<QuestionsEntity> dataList;
        try {
            dataList = FastExcel.read(file.getInputStream()).head(QuestionsEntity.class).sheet().doReadSync();
        } catch (IOException e) {
            throw new BusinessException("数据格式存在问题，无法读取");
        }

        if (CollectionUtils.isEmpty(dataList)) {
            return ResponseDTO.userErrorParam("数据为空");
        }
        QuestionsaEntity questionsaEntity = new QuestionsaEntity();


        for (QuestionsEntity questionsEntity : dataList) {
            questionsDao.insert(questionsEntity);
            int lineNum = 1;
            String option = questionsEntity.getOption();
            for (String s : option.split(",")) {
                String[] split = s.split(":");
                questionsaEntity.setDocEntry(questionsEntity.getDocEntry());
                questionsaEntity.setLineNum(lineNum++);
                questionsaEntity.setAnswer(split[1]);
                questionsaEntity.setInspect(split[0].equals("1"));
                questionsaEntity.setLineStatus("O");
                questionsaDao.insert(questionsaEntity);
            }
        }
        return ResponseDTO.okMsg("成功导入" + dataList.size() + "条，具体数据为：" + JSON.toJSONString(dataList));
    }
}
